<script setup>
import { getDetail,addCommentLikeAPI,getCommentList } from "@/apis/detail.js";
import {useRoute} from 'vue-router'
import { ref } from "vue";
import ImageView from "@/components/ImageView/index.vue";
import { useCartStore } from "@/stores/cartStore.js";
import { ElMessage } from "element-plus";
const cartStore = useCartStore();
const route=useRoute()

const count=ref(1)
const countChange=(count)=>{
  console.log("count",count);
}


const comments=ref([])
const detailData=ref({})
const ImageList=ref([])
const { id } = route.params
console.log('参数',id);
// const commentTimeList=ref([])
const func2=async()=>{
  const res = await getDetail(id)
  console.log('商品详情',res.data);
  ImageList.value=res.data.data.pictures
  console.log("图片",ImageList.value);
  const result=await getCommentList(id)
  comments.value=result.data.data
  // userInfo.value=res.data.data.comments.user
  // console.log("参数user",userInfo.value);
  console.log("参数comments",comments.value);
  // commentTimeList.value=comments.value
  detailData.value=res.data.data
  console.log("参数data",res.data.data)
}
func2()

//添加购物车
const addCart=()=>{
  console.log("添加++++++++++++++++++++++++++++")
  cartStore.addCart({
    id:detailData.value.id,
    name:detailData.value.name,
    mainPicture:detailData.value.mainPicture,
    price:detailData.value.price,
    count:count.value,
    description:detailData.value.description,
    selected:true
  })
  ElMessage.success("添加成功")
}
const addzan=async(id)=>{
  console.log("点赞",id);
  await addCommentLikeAPI(id)
  func2()
}

</script>

<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/' }">母婴
          </el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/' }">跑步鞋
          </el-breadcrumb-item>
          <el-breadcrumb-item>抓绒保暖，毛毛虫子儿童运动鞋</el-breadcrumb-item>
        </el-breadcrumb>
      </div> -->
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media">
              <!-- 图片预览区 -->
              <ImageView :ImageList="ImageList" ></ImageView>
              <!-- 统计数量 -->
              <ul class="goods-sales">
                <li>
                  <p>销量人气</p>
                  <p> {{detailData.saleCount}}+ </p>
                  <p><i class="iconfont icon-task-filling"></i>销量人气</p>
                </li>
                <li>
                  <p>商品评价</p>
                  <p>200+</p>
                  <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
                </li>
                <li>
                  <p>收藏人气</p>
                  <p>300+</p>
                  <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
                </li>
                <li>
                  <p>品牌信息</p>
                  <p>400+</p>
                  <p><i class="iconfont icon-dynamic-filling"></i>茶现武夷</p>
                </li>
              </ul>
            </div>
            <div class="spec">
              <!-- 商品信息区 -->
              <p class="g-name">{{ detailData.name }} </p>
              <p class="g-desc">{{ detailData.description }} </p>
              <p class="g-price">
                <span>{{ detailData.price }}</span>
                <span> 9999</span>
              </p>
              <div class="g-service">
                <dl>
                  <dt>促销</dt>
                  <dd>12月好物放送，App领券购买直降120元</dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>无忧退货</span>
                    <span>快速退款</span>
                    <span>免费包邮</span>
                    <a href="javascript:;">了解详情</a>
                  </dd>
                </dl>
                <dl>
                  <dt>产地</dt>
                  <dd style="color: green;font-size: 25px;">{{ detailData.productAddress }}</dd>
                </dl>
              </div>
              <!-- sku组件 -->

              <!-- 数据组件 -->
              <el-input-number :min="1" v-model="count"  @change="countChange" />
              <!-- 按钮组件 -->
              <div>
                <el-button size="large" class="btn" @click="addCart">
                  加入购物车
                </el-button>
              </div>

            </div>
          </div>
          <div class="goods-footer">
            <div class="goods-article">
              <!-- 商品详情 -->
              <div class="goods-tabs">
                <nav>
                  <a>商品评价</a>
                </nav>
                <ul>
                  <li v-for="item in comments" :key="item.id">
                    <div class="list">
                  <div class="item">
                    <div class="user">
                      <!-- <img src="http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/avatar_1.png" alt=""> -->
                      <el-avatar>
                        <img :src="item.user.avatar" alt="" />
                      </el-avatar>
                      <span>{{ item.user.nickname }}</span>
                    </div>
                    <div class="body">
                      <div class="score">
                        <!-- 评分 -->
                        <i class="iconfont icon-wjx01"></i>
                        <i class="iconfont icon-wjx01"></i>
                        <i class="iconfont icon-wjx01"></i>
                        <i class="iconfont icon-wjx01"></i>
                        <i class="iconfont icon-wjx02"></i>
                        <span class="attr">{{ detailData.name }} 产地：{{ detailData.productAddress }}</span>
                      </div>
                      <div class="text">{{ item.content }}</div>
                      <div class="time">
                        <span>{{ item.date.slice(0,10) }}</span>
                        <span class="zan"><i class="iconfont icon-dianzan"  @click="addzan(item.id)"></i>{{ item.likeNumber }}</span>
                      </div>
                    </div>
                  </div>
                </div>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>    
      </div>
    </div>
  </div>
</template>


<style scoped lang='scss'>
.xtx-goods-page {
  .goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .goods-footer {
    display: flex;
    margin-top: 20px;

    .goods-article {
      width: 940px;
      margin-right: 20px;
    }

    .goods-aside {
      width: 280px;
      min-height: 1000px;
    }
  }

  .goods-tabs {
    width: 1240px;
    min-height: 600px;
    background: #fff;
  }

  .goods-warn {
    min-height: 600px;
    background: #fff;
    margin-top: 20px;
  }

  .number-box {
    display: flex;
    align-items: center;

    .label {
      width: 60px;
      color: #999;
      padding-left: 10px;
    }
  }

  .g-name {
    font-size: 30px;
  }

  .g-desc {
    font-size: 15px;
    color: #999;
    margin-top: 15px;
  }

  .g-price {
    margin-top: 20px;
    font-size: 30px;
    span {
      &::before {
        content: "¥";
        font-size: 14px;
      }

      &:first-child {
        color: $priceColor;
        margin-right: 10px;
        font-size: 22px;
      }

      &:last-child {
        color: #999;
        text-decoration: line-through;
        font-size: 16px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;
    font-size: 20px;
    margin-bottom: 10px;
    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 50px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              content: "•";
              color: $xtxColor;
              margin-right: 2px;
            }
          }

          a {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: "";
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: $priceColor;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: $xtxColor;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: $xtxColor;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      >span {
        color: $priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
    }
  }
}

.goods-detail {
  padding: 40px;

  .attrs {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;

    li {
      display: flex;
      margin-bottom: 10px;
      width: 50%;

      .dt {
        width: 100px;
        color: #999;
      }

      .dd {
        flex: 1;
        color: #666;
      }
    }
  }

  >img {
    width: 100%;
  }
}

.btn {
  margin-top: 20px;

}

.bread-container {
  padding: 25px 0;
}

.list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }

</style>